home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZDICT < prev    next >
Text File  |  1991-10-25  |  5KB  |  243 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zdict.c */
  21. /* Dictionary operators for GhostScript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "dict.h"
  26. #include "store.h"
  27.  
  28. /* Import the dictionary stack */
  29. extern ref *dsp, *dstop;
  30.  
  31. /* dict */
  32. int
  33. zdict(register os_ptr op)
  34. {    check_type(*op, t_integer);
  35.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  36.         return e_rangecheck;
  37.     return dict_create((uint)op->value.intval, op);
  38. }
  39.  
  40. /* maxlength */
  41. int
  42. zmaxlength(register os_ptr op)
  43. {    check_type(*op, t_dictionary);
  44.     check_dict_read(*op);
  45.     make_int(op, dict_maxlength(op));
  46.     return 0;
  47. }
  48.  
  49. /* setmaxlength */
  50. int
  51. zsetmaxlength(register os_ptr op)
  52. {    uint new_size;
  53.     int code;
  54.     os_ptr op1 = op - 1;
  55.     check_type(*op1, t_dictionary);
  56.     check_dict_write(*op1);
  57.     check_type(*op, t_integer);
  58.     if ( op->value.intval < 0 || op->value.intval > dict_max_size )
  59.         return e_rangecheck;
  60.     new_size = (uint)op->value.intval;
  61.     if ( dict_length(op - 1) > new_size )
  62.         return e_dictfull;
  63.     code = dict_resize(op - 1, new_size);
  64.     if ( code >= 0 ) pop(2);
  65.     return code;
  66. }
  67.  
  68. /* begin */
  69. int
  70. zbegin(register os_ptr op)
  71. {    check_type(*op, t_dictionary);
  72.     check_dict_read(*op);
  73.     if ( dsp == dstop ) return e_dictstackoverflow;
  74.     *++dsp = *op;
  75.     pop(1);
  76.     return 0;
  77. }
  78.  
  79. /* end */
  80. int
  81. zend(register os_ptr op)
  82. {    if ( dsp == dstack + 1 ) return e_dictstackunderflow;
  83.     dsp--;
  84.     return 0;
  85. }
  86.  
  87. /* def */
  88. int
  89. zdef(register os_ptr op)
  90. {    int code;
  91.     check_op(2);
  92.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  93.     check_dict_write(*dsp);
  94.     code = dict_put(dsp, op - 1, op);
  95.     if ( !code ) pop(2);
  96.     return code;
  97. }
  98.  
  99. /* load */
  100. int
  101. zload(register os_ptr op)
  102. {    ref *pvalue;
  103.     check_op(1);
  104.     if ( r_has_type(op, t_null) ) return e_typecheck;
  105.     if ( dict_lookup(dstack, dsp, op, &pvalue) <= 0 )
  106.         return e_undefined;
  107.     ref_assign(op, pvalue);
  108.     return 0;
  109. }
  110.  
  111. /* store */
  112. int
  113. zstore(register os_ptr op)
  114. {    ref *pvalue;
  115.     int code;
  116.     check_op(2);
  117.     if ( r_has_type(op - 1, t_null) ) return e_typecheck;
  118.     if ( dict_lookup(dstack, dsp, op - 1, &pvalue) <= 0 )
  119.        {    code = dict_put(dsp, op - 1, op);
  120.         if ( code ) return code;
  121.        }
  122.     else
  123.         ref_assign_old(pvalue, op, "store");
  124.     pop(2);
  125.     return 0;
  126. }
  127.  
  128. /* get - implemented in zgeneric.c */
  129.  
  130. /* put - implemented in zgeneric.c */
  131.  
  132. /* undef */
  133. int
  134. zundef(register os_ptr op)
  135. {    check_type(op[-1], t_dictionary);
  136.     check_dict_write(op[-1]);
  137.     if ( !r_has_type(op, t_null) )
  138.         dict_undef(op - 1, op);    /* ignore undefined error */
  139.     pop(2);
  140.     return 0;
  141. }
  142.  
  143. /* known */
  144. int
  145. zknown(register os_ptr op)
  146. {    os_ptr op1 = op - 1;
  147.     ref *pvalue;
  148.     check_type(*op1, t_dictionary);
  149.     check_dict_read(*op1);
  150.     make_bool(op1,
  151.           (r_has_type(op, t_null) ? 0 :
  152.            dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  153.     pop(1);
  154.     return 0;
  155. }
  156.  
  157. /* where */
  158. int
  159. zwhere(register os_ptr op)
  160. {    ref *pdref = dsp;
  161.     ref *pvalue;
  162.     check_op(1);
  163.     if ( r_has_type(op, t_null) )
  164.        {    make_bool(op, 0);
  165.         return 0;
  166.        }
  167.     while ( 1 )
  168.        {    check_dict_read(*pdref);
  169.         if ( dict_find(pdref, op, &pvalue) > 0 ) break;
  170.         if ( --pdref < dstack )
  171.            {    make_bool(op, 0);
  172.             return 0;
  173.            }
  174.        }
  175.     ref_assign(op, pdref);
  176.     push(1);
  177.     make_bool(op, 1);
  178.     return 0;
  179. }
  180.  
  181. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  182. /* Only the type of *op has been checked. */
  183. int
  184. zcopy_dict(register os_ptr op)
  185. {    os_ptr op1 = op - 1;
  186.     check_type(*op1, t_dictionary);
  187.     check_dict_read(*op1);
  188.     check_dict_write(*op);
  189.     if ( dict_length(op) != 0 || dict_maxlength(op) < dict_maxlength(op1) )
  190.         return e_rangecheck;
  191.     dict_copy(op1, op);
  192.     ref_assign(op - 1, op);
  193.     pop(1);
  194.     return 0;
  195. }
  196.  
  197. /* currentdict */
  198. int
  199. zcurrentdict(register os_ptr op)
  200. {    push(1);
  201.     ref_assign(op, dsp);
  202.     return 0;
  203. }
  204.  
  205. /* countdictstack */
  206. int
  207. zcountdictstack(register os_ptr op)
  208. {    push(1);
  209.     make_int(op, dsp - dstack + 1);
  210.     return 0;
  211. }
  212.  
  213. /* dictstack */
  214. int
  215. zdictstack(register os_ptr op)
  216. {    int depth = dsp - dstack + 1;
  217.     check_write_type(*op, t_array);
  218.     if ( depth > r_size(op) ) return e_rangecheck;
  219.     r_set_subrange_size(op, depth);
  220.     refcpy_to_old(op->value.refs, dstack, depth, "dictstack");
  221.     return 0;
  222. }
  223.  
  224. /* ------ Initialization procedure ------ */
  225.  
  226. op_def zdict_op_defs[] = {
  227.     {"1begin", zbegin},
  228.     {"0countdictstack", zcountdictstack},
  229.     {"0currentdict", zcurrentdict},
  230.     {"2def", zdef},
  231.     {"1dict", zdict},
  232.     {"0dictstack", zdictstack},
  233.     {"0end", zend},
  234.     {"2known", zknown},
  235.     {"1load", zload},
  236.     {"1maxlength", zmaxlength},
  237.     {"2setmaxlength", zsetmaxlength},
  238.     {"2store", zstore},
  239.     {"2undef", zundef},
  240.     {"1where", zwhere},
  241.     op_def_end(0)
  242. };
  243.